package com.visual.plate.app;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.client.ClientHttpRequestFactory;
import org.springframework.http.client.SimpleClientHttpRequestFactory;
import org.springframework.http.converter.StringHttpMessageConverter;
import org.springframework.web.client.RestTemplate;

import java.nio.charset.Charset;

@Configuration
@SpringBootApplication()
public class OpenPlateServer {

    private static final Logger logger = LoggerFactory.getLogger(OpenPlateServer.class);

    public static void main(String[] args) {
        try {
            ApplicationContext context = SpringApplication.run(OpenPlateServer.class, args);
            logger.info("ActorSearch Server startup success : ApplicationContextId=" + context.getId());
        } catch (Exception e) {
            logger.error("ActorSearch Server startup failed : " + e.getMessage(), e);
        }
    }

    @Configuration
    @ComponentScan({"com.visual.plate.app"})
    public static class ControllerConfig {}


    @Bean
    public RestTemplate restTemplate(ClientHttpRequestFactory factory){
        RestTemplate restTemplate= new RestTemplate(factory);
        // 支持中文编码
        restTemplate.getMessageConverters().set(1,
                new StringHttpMessageConverter(Charset.forName("UTF-8")));
        return restTemplate;

    }

    @Bean
    public ClientHttpRequestFactory simpleClientHttpRequestFactory(){
        SimpleClientHttpRequestFactory factory = new SimpleClientHttpRequestFactory();
        factory.setReadTimeout(30000);
        factory.setConnectTimeout(15000);
        return factory;
    }

}
